# 定义一个列表来存储学生成绩信息
students_scores = []

# 记录学生成绩函数
def record_score():
    while True:
        print("=====记录学生成绩=====")
        name = input("请输入学生姓名：")
        id_num = input("请输入学生学号：")
        # 检查学号是否重复
        for student in students_scores:
            if student['学号'] == id_num:
                print("学号已存在，请重新输入！")
                break
        else:
            break
    course = input("请输入课程名称：")
    while True:
        try:
            score = float(input("请输入成绩(0-100)："))
            if 0 <= score <= 100:
                break
            else:
                print("成绩必须在0到100之间，请重新输入！")
        except ValueError:
            print("输入无效，请输入一个有效的数字！")
    students_scores.append({
        '姓名': name,
        '学号': id_num,
        '课程': course,
        '成绩': score
    })
    print("成绩已成功记录！")

# 查询学生成绩函数
def query_score():
    print("=====查询学生成绩=====")
    print("请选择查询方式：")
    print("1. 按学生姓名查询")
    print("2. 按学生学号查询")
    print("3. 按课程名称查询")
    choice = input("请输入选项序号：")
    if choice == "1":
        name = input("请输入学生姓名：")
        found = False
        for student in students_scores:
            if student['姓名'] == name:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
                found = True
        if not found:
            print(f"未找到姓名为 {name} 的学生成绩记录。")
    elif choice == "2":
        id_num = input("请输入学生学号：")
        found = False
        for student in students_scores:
            if student['学号'] == id_num:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
                found = True
        if not found:
            print(f"未找到学号为 {id_num} 的学生成绩记录。")
    elif choice == "3":
        course = input("请输入课程名称：")
        for student in students_scores:
            if student['课程'] == course:
                print(f"姓名：{student['姓名']}, 学号：{student['学号']}, 课程：{student['课程']}, 成绩：{student['成绩']}")
    else:
        print("无效的查询选项，请重新运行程序选择正确选项。")


